﻿using OsPortal;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Data;
using System.Data.SqlClient;


/// <summary>
/// Summary description for clsGiaTinVip
/// </summary>
public class clsGiaTinVip
{
    private const string strClassName = "clsGiaTinVip";
    private String _GV_ID;
    private String _gv_khuvuc;
    private Double _gv_gold;

    public clsGiaTinVip(){}
    public clsGiaTinVip(String GV_ID,String gv_khuvuc,Int32 gv_gold){_GV_ID = GV_ID; _gv_khuvuc = gv_khuvuc; _gv_gold = gv_gold;}

    public String GV_ID
    {
        get { return _GV_ID; }
        set { _GV_ID = value; }
    }

    public String gv_khuvuc
    {
        get { return _gv_khuvuc; }
        set { _gv_khuvuc = value; }
    }

    public Double gv_gold
    {
        get { return _gv_gold; }
        set { _gv_gold = value; }
    }

    /// <summary>
    /// Lấy tất cả danh sách khu vực tin vip
    /// </summary>
    /// <returns>datatabe</returns>
    public static DataTable GetAll()
    {
        // Lấy tham chiếu đến đối tượng Cache.
        Cache _cache = HttpContext.Current.Cache;
        DataTable dt = null;
        string strCacheName = "";
        strCacheName = "VITRI_VIP_All";
        if (_cache[strCacheName] == null)
        {
            dt = oSqlDataHelper.sExecuteDataTable("SELECT * FROM GIATINVIP WHERE GV_STATUS=1");
            // Lưu giữ item trong cache
            // cùng với sliding expiration là 60 phút.
            if (dt != null && dt.Rows.Count > 0)
            {
                HttpContext.Current.Cache.Insert(strCacheName, dt, null, DateTime.MaxValue, TimeSpan.FromMinutes(basConst.timeCache));
            }
        }
        else
        {
            dt = (DataTable)_cache[strCacheName];
        }

        return dt;
    }

    /// <summary>
    /// Chi tiết giá tin vip by ID
    /// Trần Văn Nguyên - 18/4/2013
    /// </summary>
    /// <param name="strGv_ID"></param>
    /// <returns></returns>
    public static DataTable getGVT_by_ID(string strGV_ID)
    {
        DataTable dt = null;
        try
        {
            dt = OsPortal.oDataHelper.filterData(GetAll(), "GV_ID='" + strGV_ID + "'");
        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "getGVT_by_ID", ex.ToString());
        }
        return dt;
    }

    /// <summary>
    /// Lấy tất cả danh sách khu vực tin vip
    /// </summary>
    /// <returns>datatabe</returns>
    public static double getGia_by_ID(string strGV_ID)
    {
        try
        {
            DataTable dt = null;
            dt = oSqlDataHelper.sExecuteDataTable("select * from giatinvip where GV_ID='" + strGV_ID + "'");
            if (dt != null)
            {
                return Convert.ToDouble(dt.Rows[0]["gv_gold"]);
            }
            return 0;
        }
        catch { return 0; }
    }


    public static bool Add(clsGiaTinVip objGiaTinVip, System.Web.UI.Page _page)
    {
        try
        {
            int i = 0;
            i = oSqlDataHelper.sExecute("GiaTinVip_Add", new SqlParameter[]{
            new SqlParameter("@GV_ID",objGiaTinVip.GV_ID),
            new SqlParameter("@gv_khuvuc",objGiaTinVip.gv_khuvuc),
            new SqlParameter("@gv_gold",objGiaTinVip.gv_gold)
            });
            if (i > 0) return true;
            return false;
        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "Add()", ex.ToString());
            return false;
        }
    }

    public static bool Update_GiaTinVip(clsGiaTinVip objGiaTinVip, System.Web.UI.Page _page)
    {
        try
        {
            int i = 0;
            i = oSqlDataHelper.sExecute("GiaTinVip_Update", new SqlParameter[]{
            new SqlParameter("@GV_ID",objGiaTinVip.GV_ID),
            new SqlParameter("@gv_khuvuc",objGiaTinVip.gv_khuvuc),
            new SqlParameter("@gv_gold",objGiaTinVip.gv_gold)
            });
            if (i > 0) return true;
            return false;

        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "Update_GiaTinVip()", ex.ToString());
            return false;
        }

    }
    public static bool Delete(string GV_ID)
    {
        int i = 0;
        try
        {
            i = oSqlDataHelper.sExecute("delete GiaTinVip where GV_ID='" + GV_ID + "'");
        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "Delete()", ex.ToString());
            return false;
        }
        if (i > 0) return true;
        else return false;

    }
    /// <summary>
    /// Kiểm tra giá phòng trọ này đã áp dụng cho phòng trọ chưa?
    /// </summary>
    /// <param name="GV_ID"></param>
    /// <returns>True nếu đã áp dụng</returns>
    public static bool CheckUsing(string GV_ID)
    {
        DataTable dt = null;
        try
        {
            dt = oSqlDataHelper.sExecuteDataTable("select count(GV_ID) total from phongtro where GV_ID='" + GV_ID + "'");
            if (dt.Rows.Count > 0)
            {
                if (Convert.ToInt32(dt.Rows[0]["total"]) > 0)
                {
                    return true;
                }
            }
            return false;
        }
        catch (Exception ex)
        {
            OsPortal.oFileHelper.WriteLogErr(strClassName, "CheckUsing()", ex.ToString());
            return false;
        }
    }
}